package com.geostar.constructionland.statistics.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.geostar.constructionland.common.entity.response.ExtJsAjaxResponse;
import com.geostar.constructionland.common.entity.statistics.PostAnalyse;
import com.geostar.constructionland.statistics.service.IPostAnalyseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * @Author makoto_zhu
 * @Date 2020-12-11 03:20:09
 * @Description
 **/
@Api("分析后置统计")
@RestController
@Slf4j
@Validated
public class PostAnalyseController {

    @Autowired
    private IPostAnalyseService postAnalyseService;

    /**
     * @param pageNo  当前页
     * @param pageNum 每页条数
     * @return
     */
    @ApiModelProperty("分页获取后置统计信息列表")
    @PostMapping(path = "/postAnalyse/list")
    public ExtJsAjaxResponse list(
            @NotNull(message = "页码不能为空") @RequestParam(value = "page") int pageNo,
            @NotNull(message = "每页数量不能为空") @RequestParam(value = "limit") int pageNum, String query_param_task_id) {
        IPage<PostAnalyse> analyseVersionIPage
                = postAnalyseService.getPostAnalyseForPage(pageNo, pageNum, query_param_task_id);
        return ExtJsAjaxResponse.createBySuccess(analyseVersionIPage);
    }

    @ApiOperation("删除后置统计实体")
    @PostMapping(path = "/postAnalyse/delete")
    public ExtJsAjaxResponse delete(@NotBlank(message = "id不能为空") String id) {
        boolean success;
        if (id.contains(",")) {
            // 将多个id处理成集合形式
            List<String> versionIds = Arrays.asList(id.split(","));
            success = postAnalyseService.removeByIds(versionIds);
        } else {
            success = postAnalyseService.removeById(id);
        }
        if (success) {
            return ExtJsAjaxResponse.createBySuccess();
        } else {
            return ExtJsAjaxResponse.createByError();
        }
    }

    @PostMapping(path = "/postAnalyse/add")
    @ApiOperation("保存后置统计实体")
    public ExtJsAjaxResponse add(@RequestBody @Valid PostAnalyse postAnalyse) {
        boolean success = postAnalyseService.save(postAnalyse);
        if (success) {
            return ExtJsAjaxResponse.createBySuccess();
        } else {
            return ExtJsAjaxResponse.createByError();
        }
    }


    @PostMapping(path = "/postAnalyse/update")
    @ApiOperation("保存后置统计实体")
    public ExtJsAjaxResponse update(@RequestBody @Valid PostAnalyse postAnalyse) {
        boolean success = postAnalyseService.updateById(postAnalyse);
        if (success) {
            return ExtJsAjaxResponse.createBySuccess();
        } else {
            return ExtJsAjaxResponse.createByError();
        }
    }


    @PostMapping(path = "/postAnalyse/startPostAnalyse")
    @ApiOperation("保存后置统计实体")
    public ExtJsAjaxResponse startPostAnalyse(String fxlx, String ywid) {
        postAnalyseService.startPostAnalyse(fxlx, ywid);
        return ExtJsAjaxResponse.createBySuccess();
    }

}
